package cn.maxwell.org.service.impl;

import cn.maxwell.basic.service.impl.BaseServiceImpl;
import cn.maxwell.org.domain.Employee;
import cn.maxwell.org.domain.Shop;
import cn.maxwell.org.domain.ShopLog;
import cn.maxwell.org.mapper.EmployeeMapper;
import cn.maxwell.org.mapper.ShopLogMapper;
import cn.maxwell.org.mapper.ShopMapper;
import cn.maxwell.org.service.IShopLogService;
import cn.maxwell.user.mapper.LogininfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class ShopLogServiceImpl extends BaseServiceImpl<ShopLog> implements IShopLogService {

    //mapper注入
    @Autowired
    private ShopLogMapper logMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private EmployeeMapper empMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;


    /**
     * 审批通过
     * @param shopLog
     * @param logininfo_id
     */
    @Transactional
    @Override
    public void pass(ShopLog shopLog, Long logininfo_id) throws MessagingException {
        //查询登录人信息
        Employee admin_id = empMapper.loadByLogininfoId(logininfo_id);
        //1.审批通过，修改店铺状态码2
        //t_shop_autid日志表通过shop_id与t_shop店铺表关联，根据日志表shop_id查询出对应店铺
        Shop shop = shopMapper.loadById(shopLog.getShop_id());
        //设置对应店铺状态码为2
        shop.setState(2);
        //更新到数据库
        shopMapper.update(shop);

        //2.将审核日志上传到日志表
        //更新日志状态
        shopLog.setState(2);
        //更新操作该店铺的员工
        shopLog.setAudit_id(admin_id.getId());
        //更新数据库日志表
        logMapper.add(shopLog);

        //审核结果:发送邮件通知审核结果
        //3.发送邮件：审核通过邮件 - 叫他激活
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方
        helper.setFrom("3254231200@qq.com");
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h1>你的店铺已经注册!!!</h1>" +
                "<img src='http://123.207.27.208"+shop.getLogo()+"' >" +
                "<a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //设置收件人
        Employee employee = empMapper.loadById(shop.getAdmin_id());
        //helper.setTo(employee.getEmail());
        //以后都是发送给店铺入驻时填写的邮箱
        helper.setTo("3254231200@qq.com");
        //发送
        javaMailSender.send(mimeMessage);
    }

    /**
     * 审批不予通过（驳回）
     * @param shopLog
     */
    @Transactional
    @Override
    public void reject(ShopLog shopLog,Long logininfo_id) throws MessagingException {
        //查询登录人信息
        Employee admin_id = empMapper.loadByLogininfoId(logininfo_id);
        //1.修改状态为4
        //t_shop_autid日志表通过shop_id与t_shop店铺表关联，根据日志表hop_id查询出对应店铺
        Shop shop = shopMapper.loadById(shopLog.getShop_id());
        //修改状态码
        shop.setState(4);
        //更新到数据库
        shopMapper.update(shop);

        //2.添加审核日志
        //添加状态
        shopLog.setState(4);
        //添加审核人员
        shopLog.setAudit_id(admin_id.getId());
        //更新到数据库
        logMapper.add(shopLog);

        //3.发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方
        helper.setFrom("3254231200@qq.com");
        //设置主题
        helper.setSubject("入驻失败邮件");
        //设置内容  跳转到注册页面
        helper.setText("<h1>入驻失败!!!</h1>" +
                "<a href='http://localhost:8081/#/register'>点击重新入驻</a>",true);
        //设置收件人
        Employee employee = empMapper.loadById(shop.getAdmin_id());
        //以后都是发送给店铺入驻时填写的邮箱
        //helper.setTo(employee.getEmail());
        helper.setTo("3254231200@qq.com");
        //发送
        javaMailSender.send(mimeMessage);

        //删除入驻信息
        //删除logininfo登录信息
        Long id = employee.getLogininfo_id();
        logininfoMapper.delete(id);
        //删除员工表管理员信息
        empMapper.delete(shop.getAdmin_id());
        //删除店铺信息
        shopMapper.delete(shop.getId());
    }
}
